package com.linchong.concurrency.chapter6;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author:linchong
 * @CreateTime:2020-01-04 09:36
 * @Description:优雅的关闭线程，通过开关的方式
 */
@Slf4j
public class ThreadCloseGraceful {
	private static class Worker extends Thread {
		private volatile boolean start = true;

		@Override
		public void run() {
			while (start) {

			}
		}

		public void shutdown() {
			this.start = false;
		}
	}

	public static void main(String[] args) {
		Worker worker = new Worker();
		worker.start();
		try {
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		worker.shutdown();
	}
}
